<template>
  <!-- 主体部分 -->
  <section class="main">
    <!-- 全选反选按钮 -->
    <input id="toggle-all" class="toggle-all" type="checkbox" v-model="AllIn" />
    <label for="toggle-all">Mark all as complete</label>
    <ul class="todo-list">
      <!-- 当任务已完成，可以给 li 加上 completed 类，会让元素加上删除线 -->
      <li v-for="item in list" :key="item.id" :class="{ completed: item.flag }">
        <div class="view">
          <input
            class="toggle"
            type="checkbox"
            :checked="item.flag"
            @change="changeEvent(item.id)"
          />
          <label>{{ item.name }}</label>
          <button class="destroy" @click="del(item.id)"></button>
        </div>
      </li>
    </ul>
  </section>
</template>

<script>
export default {
  props: {
    list: {
      type: Array,
      required: true,
    },
  },
  methods: {
    changeEvent(id) {
      this.$emit("changeEvent", id);
    },
    del(id) {
      this.$emit("remove", id);
    },
  },
  computed: {
    AllIn: {
      get() {
        return this.list.every((item) => item.flag === true);
      },
      set(value) {
        // return this.list.forEach((item) => (item.flag = value));
        this.$emit("allIn", value);
      },
    },
  },
};
</script>

<style></style>